package com.hncj.yqfk.controller;

import com.hncj.yqfk.aop.LogAnnotation;
import com.hncj.yqfk.entity.USER;
import com.hncj.yqfk.service.impl.UserServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@Controller
@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    private UserServiceImp usi;

    @RequestMapping("/login")
    @LogAnnotation(operModul = "管理员模块", operType = "登录", operDesc = "管理员登录")
    public String login(@Param("username") String username, @Param("password") String password, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        USER user = usi.findUserByUsernameAndPassword(username, password);

        if (user != null) {
            session.setAttribute("loginUser", username);
            response.sendRedirect("/index.html");
            request.setAttribute("user_code", username);
        } else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('账号密码错误！！请重新登录！'); window.location='/login/login.html'; window.close();</script>");
            response.getWriter().flush();
        }
        return username;
    }
    @LogAnnotation(operModul = "注册模块", operType = "注册", operDesc = "管理员注册账号")
    @RequestMapping("/register")
    public boolean Adduser(USER user, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        List<USER> existUser=usi.findByName(username);
        if (username.equals("") | password.equals("")){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('请填写您的账号密码'); window.location='/login/login.html'; window.close();</script>");
            response.getWriter().flush();
            return false;
        }
        else {
            if (existUser.size()>0){
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<script>alert('该用户已存在！！将跳转到登录页面！'); window.location='/login/login.html'; window.close();</script>");
                response.getWriter().flush();
                return false;
            }

            else {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("<script>alert('恭喜您，注册成功！将跳转到登录页面！'); window.location='/login/login.html'; window.close();</script>");
                response.getWriter().flush();
                return usi.addUser(user);

            }

        }
    }






//    public String register(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        if (registerUser(username) == true && username !=null && password !=null) {
//            USER user1 = new USER();
//            user1.setUsername(username);
//            user1.setPassword(password);
//            usi.addUser(user1);
//            response.sendRedirect("/login/login.html");
//            return username;
//        } else if (registerUser(username) == false) {
//            response.getWriter().write("<script>alert('该用户已存在！');</script>");
//            response.sendRedirect("/login/login.html");
//            return username;
//        } else {
//            response.sendRedirect("/login/regist.html");
//            return username;
//        }
//    }
//    public Boolean registerUser(String username)  {
//        Boolean a = true;
//        if (usi.findByName(username).isEmpty()) {
//            return a;
//        } else {
//            return false;
//        }
//    }
}
